﻿using CIS.Web.Common;
using CIS.Web.Models;
using CIS.Web.Properties;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[RoutePrefix("SysStatistics")]
	public class SysStatisticsController : BaseDbController
	{
		public ActionResult Index()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			return View();
		}
		
		public async Task<JsonCamelCaseResult> GetAttendencePerson(int? page, int? rows, string sort, string order, SysStatisticsModel model)
		{
			int take = rows ?? ConstValues.DefaultGridRows;
			int skip = ((page ?? 1) - 1) * take;

			var sql = DbContext.ViewPersonAttendence.AsNoTracking().Where(x => !string.IsNullOrEmpty(x.UserName));

			if (!string.IsNullOrWhiteSpace(model.UserName))
			{
				sql = sql.Where(x => x.UserName.Contains(model.UserName));
			}

			int total = await sql.CountAsync();

			if (!string.IsNullOrEmpty(sort))
			{
				sql = sql.OrderBy(sort + ConstValues.chrSpace + order).Skip(skip).Take(take);
			}
			else
			{
				sql = sql.OrderBy(x => x.UserName).Skip(skip).Take(take);
			}

			var sqlResult = await sql.ToListAsync();

			return JsonCamelCase(new
			{
				Total = total,
				Rows = sqlResult,
			},
			JsonRequestBehavior.AllowGet);
		}

		public async Task<JsonCamelCaseResult> GetAttendenceCareer(int? page, int? rows, string sort, string order, SysStatisticsModel model)
		{
			int take = rows ?? ConstValues.DefaultGridRows;
			int skip = ((page ?? 1) - 1) * take;
			
			var sql = DbContext.ViewPersonAttendenceCareer.AsNoTracking().Where(x => !string.IsNullOrEmpty(x.CareerName));

			if (model.CareerId.HasValue && model.CareerId.Value != 0)
			{
				sql = sql.Where(x => x.ID == model.CareerId.Value);
			}

			int total = await sql.CountAsync();

			if (!string.IsNullOrEmpty(sort))
			{
				sql = sql.OrderBy(sort + ConstValues.chrSpace + order).Skip(skip).Take(take);
			}
			else
			{
				sql = sql.OrderBy(x => x.CareerName).Skip(skip).Take(take);
			}

			var sqlResult = await sql.ToListAsync();

			return JsonCamelCase(new
			{
				Total = total,
				Rows = sqlResult,
			},
			JsonRequestBehavior.AllowGet);
		}

		public async Task<JsonCamelCaseResult> GetAttendenceIdentity(int? page, int? rows, string sort, string order, SysStatisticsModel model)
		{
			int take = rows ?? ConstValues.DefaultGridRows;
			int skip = ((page ?? 1) - 1) * take;

			var sql = DbContext.ViewPersonAttendenceIdentity.AsNoTracking().Where(x => !string.IsNullOrEmpty(x.IdentityName));

			if (!string.IsNullOrWhiteSpace(model.AttendenceIdentity) && model.AttendenceIdentity != Resources.All)
			{
				sql = sql.Where(x => x.IdentityName == model.AttendenceIdentity);
			}

			int total = await sql.CountAsync();

			if (!string.IsNullOrEmpty(sort))
			{
				sql = sql.OrderBy(sort + ConstValues.chrSpace + order).Skip(skip).Take(take);
			}
			else
			{
				sql = sql.OrderBy(x => x.IdentityName).Skip(skip).Take(take);
			}

			var sqlResult = await sql.ToListAsync();

			return JsonCamelCase(new
			{
				Total = total,
				Rows = sqlResult,
			},
			JsonRequestBehavior.AllowGet);
		}
	}
}